const _import = require('@/router/_import_' + process.env.NODE_ENV);
import Layout from '@/views/layout/Layout';
import SubLayout from '@/views/layout/SubLayout';

import {
  asyncRouterMap,
  constantRouterMap
} from 'src/router'
import { getAllMenus } from 'api/login';
/**
 * 通过authority判断是否与当前用户权限匹配
 * @param menus
 * @param route
 */
function hasPermission(menus, route) {
  if (route.authority) {
    if (menus[route.authority] !== undefined) {
      return menus[route.authority];
    } else {
      return false;
    }
  } else {
    return true;
  }
}

/**
 * 递归过滤异步前端路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param menus
 * @param menuDatas
 */
function filterAsyncRouter(asyncRouterMap, menus, menuDatas) {
  // menus-用户权限 数据格式：{code：true}
  // menuDatas-所有菜单 数据格式：{code：menu}
  // asyncRouterMap-所有菜单数据
  const accessedRouters = asyncRouterMap.filter(route => {
    // console.log('route is:');
    // console.log(route);
    // console.log('menus is:');
    // console.log(menus);
    if (hasPermission(menus, route)) {
      // route.name = menuDatas[route.authority].title;
      // route.icon = menuDatas[route.authority].icon;
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, menus, menuDatas);
      }
      return true
    }
    return false
  })
  return accessedRouters
}
/**
 * 从后台获取接口所有权限的数据
 * @param {*} 
 * @param {*} 
 */
function filterRouter(allMenusTree, menus) {
  const accessedRouters = allMenusTree.filter(menu => {
    if (menu.component) {
      if (menu.component === 'Layout') {
        menu.component = Layout;
        menu.path = '/' + menu.path;
      } else if (menu.component === 'SubLayout') {
        menu.component = SubLayout;
        menu.path = '/' + menu.path;
      } else {
        menu.component = _import(menu.component)
      }
    }
    delete menu.id;
    delete menu.parentId;
    if (hasPermission(menus, menu)) {
      // route.name = menuDatas[route.authority].title;
      // route.icon = menuDatas[route.authority].icon;
      if (menu.children && menu.children.length) {
        menu.children = filterRouter(menu.children, menus);
      }
      return true;
    }
    return false;
  })
  return accessedRouters
}
const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({
      commit
    }, menus) {
      return new Promise(resolve => {
        getAllMenus().then(data => {
          const menuDatas = {};
          for (let i = 0; i < data.length; i++) {
            // console.log(data[i]);
            menuDatas[data[i].code] = data[i];
          }
          // console.log('before asyncRouterMap:');
          // console.log(asyncRouterMap);
          // console.log('before menus:');
          // console.log(menus);
          // console.log('before menuDatas:');
          // console.log(menuDatas);
          // console.log('data is:');
          // console.log(data);
          // menus-用户权限 数据格式：{code：true}
          // menuDatas-所有菜单 数据格式：{code：menu}
          // asyncRouterMap-所有菜单数据
          // const accessedRouters = filterAsyncRouter(asyncRouterMap, menus, menuDatas);

          const accessedRouters = filterRouter(data, menus);
          console.log('after accessedRouters is:');
          console.log(accessedRouters);
          // 添加到路由
          commit('SET_ROUTERS', accessedRouters);
          resolve();
        });
      })
    }
  }
};
export default permission;
